<template>
    <div>
        欢迎你：{{username}}
        <p v-for="(goods, index) in goodslist" :key="index">
            {{goods.title}}---{{goods.price}}---<button @click="buy">购买</button>
        </p>
    </div>
</template>

<script>
export default {
    data() {
        return {
            goodslist:[],
            username:localStorage.getItem('username'),
            user_level:localStorage.getItem('user_level')
        }
    },
    mounted:function(){
        this.axios.get('http://localhost:8000/goods/').then(res=>{
            console.log(res.data)
            this.goodslist = res.data
        })
    },
    methods:{
        buy(){
            if (this.user_level==1){
                var prices = this.price - 10
            }
            else{
                var prices = this.price
            }
            this.axios.post('http://localhost:8000/goods/', this.qs.stringify({'title': this.title,'price': prices})).then(res=>{
                console.log(res.data)
                
                this.$toast('购买成功')
            })
        }
    }
}
</script>

<style>

</style>